﻿using System;
using System.Data;
using System.Globalization;

using DayPilot.Web.Ui.Events;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.TA
{
    public partial class Schedule : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        //string enteredID;

        string id;
        DataTable table;

        string strProvider = Resource1.databaseConnection;

        protected void Page_Load(object sender, EventArgs e)
        {
 
            ((Menu)Master.FindControl("Menu1")).Items[2].Selected = true;

            conn = new MySqlConnection(strProvider);
            conn.Open();

            id = (string)Session["userName"];
            if (id == null)
                Response.Redirect("../login.aspx");


            string getSelectedInfo = "SELECT S.schId, S.sch_startTime, S.sch_endTime "
                                 + "FROM schedule S, hasschedule H  "
                                 + "WHERE S.schId = H.schId  AND H.sid = " + id;

            sqlCommand = new MySqlCommand(getSelectedInfo, conn);
            readVals = sqlCommand.ExecuteReader();


            DataTable dt;
            dt = new DataTable();
            dt.Columns.Add("start", typeof(DateTime));
            dt.Columns.Add("end", typeof(DateTime));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("id", typeof(string));

            DataRow dr;

            while (readVals.Read())
            {
                dr = dt.NewRow();
                dr["id"] = readVals.GetString("schId");

                dr["start"] = Convert.ToDateTime(readVals.GetString("sch_startTime"));
                dr["end"] = Convert.ToDateTime(readVals.GetString("sch_endTime"));

                dr["name"] = "Free";
                dt.Rows.Add(dr);
            }

            dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };

            table = dt;
            DayPilotCalendar1.DataSource = table;

            DayPilotCalendar1.DataBind();
            //DayPilotCalendar1.Update();
            if (!IsPostBack)
            {
                DayPilotCalendar1.StartDate = DayPilot.Utils.Week.FirstDayOfWeek(DateTime.Today);
                DataBind();
            }
            conn.Close();
        }

        
        protected void DayPilotCalendar1_TimeRangeSelected(object sender, TimeRangeSelectedEventArgs e)
        {

        }



        protected void DayPilotCalendar1_TimeRangeMenuClick(object sender, TimeRangeMenuClickEventArgs e)
        {
            if (e.Command == "Insert")
            {
               #region Simulation of database update
                DataRow dr = table.NewRow();
                dr["start"] = e.Start;
                dr["end"] = e.End;
                //dr["id"] = Guid.NewGuid().ToString();
                dr["name"] = "Free";
                
                

                conn = new MySqlConnection(strProvider);
                conn.Open();
                string modifyRa = "INSERT INTO  schedule(sch_startTime, sch_endTime) "
                                      + "VALUES( '" + e.Start.ToString("yyyy-MM-dd HH:mm") + "', '" + e.End.ToString("yyyy-MM-dd HH:mm") + "')";
                sqlCommand = new MySqlCommand(modifyRa, conn);
                sqlCommand.ExecuteNonQuery();

                conn = new MySqlConnection(strProvider);
                conn.Open();
                modifyRa = "SELECT MAX(schId) AS s "
                                    + "FROM schedule ";
                sqlCommand = new MySqlCommand(modifyRa, conn);
                readVals = sqlCommand.ExecuteReader();

                string sch;
                readVals.Read();
                sch = "" + readVals.GetString("s");
                conn.Close();

                dr["id"] = sch;


                conn = new MySqlConnection(strProvider);
                conn.Open();
                modifyRa = "INSERT INTO  HasSchedule "
                                    + "VALUES( " + sch + "," +  id + " )";
                sqlCommand = new MySqlCommand(modifyRa, conn);
                sqlCommand.ExecuteNonQuery();
                conn.Close();

                table.Rows.Add(dr);
                table.AcceptChanges();
                #endregion


                DayPilotCalendar1.DataBind();
                 DayPilotCalendar1.Update();

            }
        }

        protected void DayPilotCalendar1_EventDelete(object sender, DayPilot.Web.Ui.Events.EventDeleteEventArgs e)
        {
            #region Simulation of database update
            
            DataRow dr = table.Rows.Find(e.Value);
            if (dr != null)
            {
                table.Rows.Remove(dr);
                table.AcceptChanges();
            }


            conn = new MySqlConnection(strProvider);
            conn.Open();
            string modifyRa = "DELETE FROM hasschedule "
                                  + "WHERE schId =" + e.Value.ToString() +" AND sid =" + id;
            sqlCommand = new MySqlCommand(modifyRa, conn);
            sqlCommand.ExecuteNonQuery();

            conn.Close();

            conn.Open();
            modifyRa = "SELECT * " +
                               "FROM hasschedule " +
                                "WHERE schId =" + e.Value.ToString();
            sqlCommand = new MySqlCommand(modifyRa, conn);
            readVals = sqlCommand.ExecuteReader();
            bool test = readVals.HasRows;
            conn.Close();
            
            

            if (!test)
            {
                conn = new MySqlConnection(strProvider);
                conn.Open();
                modifyRa = "DELETE FROM schedule "
                                      + "WHERE sch_startTime ='" + e.Start.ToString("yyyy-MM-dd hh:mm") + "' AND sch_endTime ='" + e.End.ToString("yyyy-MM-dd hh:mm") + "' AND schId=" + e.Value.ToString();
                sqlCommand = new MySqlCommand(modifyRa, conn);
                sqlCommand.ExecuteNonQuery();
                conn.Close();
            }

            

            #endregion

          //  Response.Redirect("RA_Schedule.aspx");
            DayPilotCalendar1.DataBind();
            DayPilotCalendar1.Update();
        }

    
    }
}